package logic

import (
	"context"
	"fmt"
	"project/model/mysql"
	"project/pkg"

	"project/projectrpc/internal/svc"
	"project/projectrpc/projectrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserLoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogic {
	return &UserLoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserLoginLogic) UserLogin(in *projectrpc.UserLoginRequest) (*projectrpc.UserLoginResponse, error) {
	// todo: add your logic here and delete this line

	tel := in.Tel
	var user mysql.User
	err := mysql.DB.Model(mysql.User{}).Where("tel=?", tel).First(&user).Error
	if err != nil {
		return nil, fmt.Errorf("手机号输入错误，该用户不存在")
	}
	password1 := pkg.Md5(in.Password)
	if password1 != user.Password {
		return nil, fmt.Errorf("登录失败，密码输入错误")
	}
	return &projectrpc.UserLoginResponse{Id: int64(user.ID)}, nil
}
